package com.timmy.test1.view;

import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.timmy.test1.App;
import com.timmy.test1.R;

/**
 * Created by timmy on 2020/5/7.
 */

public class SupportLineView extends RelativeLayout implements View.OnTouchListener {
    private int endX;

    private boolean isMoved = false;

    private int lastX;

    private int lastY;

    private ViewGroup.MarginLayoutParams layoutParams;

    protected int leftMargin;

    private OnSupportLineViewClickListener mLister;

    public int mViewHeight;

    public int mViewWidth;

    protected int orientation;

    private int screenHeight;

    private int screenWidth;

    private int startX;

    protected int topMargin;

    public SupportLineView(Context paramContext) {
        super(paramContext);
        init(paramContext);
    }

    public SupportLineView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        init(paramContext);
    }

    public SupportLineView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
        init(paramContext);
    }

    private void getWH() {
        Display display = ((WindowManager) getContext().getSystemService("window")).getDefaultDisplay();
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= 19) {
            display.getRealSize(point);
        } else {
            display.getSize(point);
        }
        this.screenHeight = point.y;
        this.screenWidth = point.x;
    }

    private void init(Context paramContext) {
        LayoutInflater.from(paramContext).inflate(R.layout.view_support, (ViewGroup) this);
        setOnTouchListener(this);
        getWH();
        post(new Runnable() {
            @Override
            public void run() {
                SupportLineView supportLineView = SupportLineView.this;
                supportLineView.mViewHeight = supportLineView.getHeight();
                supportLineView = SupportLineView.this;
                supportLineView.mViewWidth = supportLineView.getWidth();
                supportLineView = SupportLineView.this;
                supportLineView.leftMargin = supportLineView.getLeft();
                supportLineView = SupportLineView.this;
                supportLineView.topMargin = supportLineView.getTop();
            }
        });
    }

    public int getLeftMargin() {
        return this.leftMargin;
    }

    public int getOrientation() {
        return this.orientation;
    }

    public int getTopMargin() {
        return this.topMargin;
    }

    @Override
    public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
        int i = paramMotionEvent.getAction();
        if (i != 0) {
            if (i != 1) {
                if (i != 2)
                    return true;
                this.isMoved = true;
                i = (int) paramMotionEvent.getRawX() - this.lastX;
                int j = (int) paramMotionEvent.getRawY() - this.lastY;
                this.leftMargin = paramView.getLeft() + i;
                this.topMargin = paramView.getTop() + j;
                i = paramView.getRight() + i;
                int k = paramView.getBottom() + j;
                if (this.leftMargin < 0) {
                    this.leftMargin = 0;
                    i = 0 + this.mViewWidth;
                }
                int m = this.screenWidth;
                j = i;
                if (i > m) {
                    this.leftMargin = m - this.mViewWidth;
                    j = m;
                }
                i = k;
                if (this.topMargin < 0) {
                    this.topMargin = 0;
                    i = paramView.getHeight() + 0;
                }
                m = this.screenHeight;
                k = i;
                if (i > m) {
                    this.topMargin = m - paramView.getHeight();
                    k = m;
                }
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("layout: ");
                stringBuilder.append(this.leftMargin);
                stringBuilder.append(" ");
                stringBuilder.append(this.topMargin);
//                Timber.e(stringBuilder.toString(), new Object[0]);
                paramView.layout(this.leftMargin, this.topMargin, j, k);
                this.lastX = (int) paramMotionEvent.getRawX();
                this.lastY = (int) paramMotionEvent.getRawY();
                return true;
            }
            if (this.isMoved) {
                ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
                this.layoutParams = marginLayoutParams;
                marginLayoutParams.topMargin = this.topMargin;
                this.layoutParams.leftMargin = this.leftMargin;
                setLayoutParams((ViewGroup.LayoutParams) this.layoutParams);
            }
            i = (int) paramMotionEvent.getRawX();
            this.endX = i;
            if (Math.abs(this.startX - i) < 6)
                return false;
        } else {
            this.lastX = (int) paramMotionEvent.getRawX();
            this.lastY = (int) paramMotionEvent.getRawY();
            this.startX = this.lastX;
        }
        return true;
    }

    public boolean onTouchEvent(MotionEvent paramMotionEvent) {
        this.mLister.onSupportLineViewClick(this);
        return true;
    }

    public void setLeftMargin(int paramInt) {
        this.leftMargin = paramInt;
    }

    public void setOnSupportLineViewClickListener(OnSupportLineViewClickListener paramOnSupportLineViewClickListener) {
        this.mLister = paramOnSupportLineViewClickListener;
    }

    public void setOrientation(int paramInt) {
        this.orientation = paramInt;
        if (paramInt == 1) {
            RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(2, -1);
            StringBuilder stringBuilder1 = new StringBuilder();
            stringBuilder1.append("w:");
            stringBuilder1.append(getWidth());
//            Timber.e(stringBuilder1.toString(), new Object[0]);
            layoutParams1.leftMargin = 30;
            findViewById(R.id.view).setLayoutParams(layoutParams1);
            return;
        }
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(-1, 2);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("h:");
        stringBuilder.append(getHeight());
//        Timber.e(stringBuilder.toString(), new Object[0]);
        layoutParams.topMargin = 30;
        findViewById(R.id.view).setLayoutParams(layoutParams);
    }

    public void setTopMargin(int paramInt) {
        this.topMargin = paramInt;
    }

    public static interface OnSupportLineViewClickListener {
        void onSupportLineViewClick(SupportLineView param1SupportLineView);
    }
}

